fix(ai-adapters): cancel orphaned stream handlers on turn cancel - #1302
Merged
Conversation
- manage provider stream handler lifecycle in execute_sse_request - cancel background SSE handlers when the response stream is dropped - stop provider adapters from spawning detached stream tasks - add coverage for managed stream cancellation on drop
wsp1911
marked this pull request as ready for review
June 24, 2026 11:19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stop detached provider stream handlers from continuing to wait for SSE / TTFT after a dialog turn has already been cancelled.
Fixes #
Type and Areas
Type:
bug fix / regression fix / test / refactor
Areas:
AI adapters
Motivation / Impact
After a user cancelled a dialog turn, the top-level stream processor stopped correctly, but the provider-side SSE handler could remain alive in the background and continue waiting for TTFT or later stream events. That produced late timeout errors and kept network work alive after the user had already cancelled the turn.
This change moves stream-handler lifecycle ownership into
execute_sse_request, wraps the returned stream in a managed stream type, and cancels the underlying handler task when the response stream is dropped. Provider adapters now return handler futures instead of spawning detached tasks themselves.Impact:
Verification
cargo test -p bitfun-ai-adapters managed_response_stream_drop_cancels_handler_taskcargo test -p bitfun-ai-adapters ttft_timeout_waits_for_first_effective_stream_output_not_http_200 -- --exactcargo check -p bitfun-ai-adaptersReviewer Notes
app.log, but that exact end-to-end manual repro was not rerun after the code change in this pass.Checklist